package com.julian.warehouse.activity;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

import com.julian.warehouse.R;
import com.julian.warehouse.base.SPBaseActivity;
import com.julian.warehouse.bean.GoodsBean;
import com.julian.warehouse.database.GoodsTable;
import com.julian.warehouse.database.MyDBOpenHelper;
import com.julian.warehouse.database.RecordsTable;
import com.julian.warehouse.dialog.MyAlertDialog;
import com.julian.warehouse.utils.DateUtil;
import com.julian.warehouse.utils.MyLog;

public class AddnewgoodsActivity extends SPBaseActivity implements View.OnClickListener {


    private SQLiteDatabase db;
    private EditText et_gname;
    private EditText et_ruku_count;
    private EditText et_cost;
    private EditText et_price;
    private EditText et_description;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_addnewgoods);
        super.init();
        setHeadTitle(true,"添加新产品");
    }

    @Override
    public void initView() {
        et_gname = (EditText) findViewById(R.id.et_gname);
        et_ruku_count = (EditText) findViewById(R.id.et_ruku_count);
        et_cost = (EditText) findViewById(R.id.et_cost);
        et_price = (EditText) findViewById(R.id.et_price);
        et_description = (EditText) findViewById(R.id.et_description);

        findViewById(R.id.btn_ruku).setOnClickListener(this);

    }

    @Override
    public void initData() {

        MyDBOpenHelper myDBOpenHelper = new MyDBOpenHelper(this);
        db = myDBOpenHelper.getReadableDatabase();

    }

    @Override
    public void initEvent() {

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_ruku:
                final String[] info = getInfo();
                if (info!=null){

                    if(queryIfGoodsExist(info[0])){
                        showMyDialog(true, "仓库中有同名产品，继续操作将覆盖原有数据，是否覆盖？", "确定", new MyAlertDialog.OnMyClickListener() {
                            @Override
                            public void onClick(MyAlertDialog dialog) {
                                dialog.dismiss();
                                executeUpdate(info);

                            }
                        }, "取消", new MyAlertDialog.OnMyClickListener() {
                            @Override
                            public void onClick(MyAlertDialog dialog) {
                                dialog.dismiss();
                            }
                        });
                    }else {
                        executeAddNew(info);
                    }

                }
                break;
            default:break;
        }
    }

    private String[] getInfo() {
        String[] info = new String[5];

        String gname = et_gname.getText().toString().trim();
        String ruku_count = et_ruku_count.getText().toString().trim();
        String cost = et_cost.getText().toString().trim();
        String price = et_price.getText().toString().trim();
        String description = et_description.getText().toString().trim();

        if(gname.equals("")){
            showToast("产品名称不能为空");
            return null;
        }
        if(ruku_count.equals("")){
            showToast("入库数量不能为空");
            return null;
        }
        if(cost.equals("")){
            showToast("成本价不能为空");
            return null;
        }
        if(price.equals("")){
            showToast("售价不能为空");
            return null;
        }

        info[0]=gname;
        info[1]=ruku_count;
        info[2]=cost;
        info[3]=price;
        info[4]=description;

        return info;
    }

    private boolean queryIfGoodsExist(String gname){

        String sql = "select * from "+GoodsTable.TBL_NAME+" where "+GoodsTable.COL_GNAME+" = "+"'"+gname+"'";
        Cursor cursor = db.rawQuery(sql, null);
        //db.close();
        if (cursor.getCount()>0){
            return true;
        }


        return false;
    }



    private void executeAddNew(String[] info){
        long timeStamp = System.currentTimeMillis();
        String time = DateUtil.getStrTimeFromStack(timeStamp + "", "yyyy-MM-dd HH:mm:ss");

        GoodsBean goodsBean = new GoodsBean(info[0],time,"picbase64",Integer.parseInt(info[1]),info[4],Double.parseDouble(info[3]),Double.parseDouble(info[2]));

        ContentValues values = new ContentValues();
        values.put(GoodsTable.COL_GNAME,goodsBean.getGname());
        values.put(GoodsTable.COL_CTIME,goodsBean.getCtime());
        values.put(GoodsTable.COL_PICURL,goodsBean.getPicurl());
        values.put(GoodsTable.COL_COUNT,goodsBean.getCount());
        values.put(GoodsTable.COL_DESCRIPTION,goodsBean.getDescription());
        values.put(GoodsTable.COL_PRICE, goodsBean.getPrice());
        values.put(GoodsTable.COL_COST, goodsBean.getCost());
        long id = db.insert(
                GoodsTable.TBL_NAME,
                null,
                values
        );
        //db.close();

        MyLog.i("AA","入库结果="+id);


        if(id!=-1){
            showToast("入库成功！");
            RukuActivity.instance.finish();
            finish();
        }else {
            showToast("入库失败，请重试！");
        }


        //将记录添加进records表

        ContentValues values2 = new ContentValues();
        values2.put(RecordsTable.COL_GNAME,goodsBean.getGname());
        values2.put(RecordsTable.COL_CTIME,time);
        values2.put(RecordsTable.COL_COUNT,goodsBean.getCount());
        values2.put(RecordsTable.COL_FLAG,0);//0代表入库操作，1出库
        values2.put(RecordsTable.COL_RESULT,id!=-1?"成功":"失败");//入库成功or失败
        long id2 = db.insert(
                RecordsTable.TBL_NAME,
                null,
                values2
        );

        Log.i("AA","入库操作添加进记录表结果---id="+id2);



    }


    private void executeUpdate(String[] info){
        long timeStamp = System.currentTimeMillis();
        String time = DateUtil.getStrTimeFromStack(timeStamp + "", "yyyy-MM-dd HH:mm:ss");

        GoodsBean goodsBean = new GoodsBean(info[0],time,"picbase64",Integer.parseInt(info[1]),info[4],Double.parseDouble(info[3]),Double.parseDouble(info[2]));

        ContentValues values = new ContentValues();
        values.put(GoodsTable.COL_GNAME,goodsBean.getGname());
        values.put(GoodsTable.COL_CTIME,goodsBean.getCtime());
        values.put(GoodsTable.COL_PICURL,goodsBean.getPicurl());
        values.put(GoodsTable.COL_COUNT,goodsBean.getCount());
        values.put(GoodsTable.COL_DESCRIPTION,goodsBean.getDescription());
        values.put(GoodsTable.COL_PRICE, goodsBean.getPrice());
        values.put(GoodsTable.COL_COST, goodsBean.getCost());

        int id = db.update(GoodsTable.TBL_NAME, values, GoodsTable.COL_GNAME + " = ?", new String[]{goodsBean.getGname()});


        if(id!=-1){
            showToast("入库成功！");
            RukuActivity.instance.finish();
            finish();
        }else {
            showToast("入库失败，请重试！");
        }

    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        db.close();
    }
}
